package com.meilingcloud.biobank.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilingcloud.biobank.domain.SampleBackup;
import com.meilingcloud.biobank.domain.SampleTrash;
import com.meilingcloud.biobank.mapper.SampleTrashMapper;
import com.meilingcloud.biobank.service.ISampleBackupService;
import com.meilingcloud.biobank.service.ISampleTrashService;
import com.meilingcloud.common.enums.SampStatue;
import com.meilingcloud.common.enums.StockState;
import com.meilingcloud.common.exception.CustomException;
import com.meilingcloud.common.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 样本回收站 Service业务层处理
 *
 * @author chenl
 * @date 2021-04-01
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Service
public class SampleTrashServiceImpl extends ServiceImpl<SampleTrashMapper, SampleTrash> implements ISampleTrashService {

    private final ISampleBackupService sampleBackupService;

    @Override
    public List<SampleTrash> queryList(SampleTrash sampleTrash) {
        LambdaQueryWrapper<SampleTrash> lqw = Wrappers.lambdaQuery();
        if (sampleTrash.getSampBackupId() != null) {
            lqw.eq(SampleTrash::getSampBackupId, sampleTrash.getSampBackupId());
        }
        if (StringUtils.isNotBlank(sampleTrash.getSampBackupCode())) {
            lqw.eq(SampleTrash::getSampBackupCode, sampleTrash.getSampBackupCode());
        }
        if (sampleTrash.getSampTypeId() != null) {
            lqw.eq(SampleTrash::getSampTypeId, sampleTrash.getSampTypeId());
        }
        return this.list(lqw);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean reduce(List<SampleTrash> sampleTrashs) {
        if (CollectionUtil.isEmpty(sampleTrashs)) {
            throw new CustomException("请选择要还原的样本");
        }
        // 删除回收站数据
        this.removeByIds(sampleTrashs.stream().map(SampleTrash::getSampTrashId).collect(Collectors.toList()));
        // 样本复份更改删除标志、更改状态为待入库
        List<SampleBackup> sampleBackupList = sampleTrashs.stream().map(item -> {
            SampleBackup sampleBackup = new SampleBackup();
            sampleBackup.setSampBackupId(item.getSampBackupId());
            sampleBackup.setSampState(SampStatue.TO_IN.getCode());
            sampleBackup.setStockState(StockState.NORMAL.getCode());
            sampleBackup.setUpdateBy(SecurityUtils.getUsername());
            return sampleBackup;
        }).collect(Collectors.toList());
        return sampleBackupService.updateBatchById(sampleBackupList);
    }
}
